package cn.fl.demo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.condition.SysCdMapDQueryCondition;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.demo.condition.DemoQueryCondition;
import cn.fl.demo.dto.DemoDTO;
import cn.fl.demo.service.IDemoService;
import cn.fl.demo.vo.DemoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.vo.SysCdMapDVO;
import net.sf.json.JSONArray;

/**
 * Demo 控制层
 * 
 * @author FUJINGRUN
 *
 */
@Controller
@RequestMapping("/demo/demo")
public class DemoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(DemoController.class);

	/**
	 * Demo服务层
	 */
	@Autowired
	private IDemoService demoService;
	
	
	@RequestMapping(value = "/toDynamicPage.do", method = { RequestMethod.GET,RequestMethod.POST })
	public ModelAndView toDynamicPage(HttpServletRequest request, HttpServletResponse response) {
		//String val = request.getParameter("val");
		ModelAndView view = new ModelAndView("/demo/demo/dynamicPage");
		/*if(StringUtils.isBlank(val)){
			val = "eduLvlCd";
		}
		BaseLogUtils.println("val："+val);
		view.addObject("eduLvlCd", val);*/
		
		/*List<String> orgCds = new ArrayList<>();
		orgCds.add("test1");
		orgCds.add("test2");
		
		view.addObject("orgCds", orgCds);*/
		return view;
	}
	
	@RequestMapping(value = "/toDynamicBizPage.do", method = { RequestMethod.GET,RequestMethod.POST })
	public ModelAndView toDynamicBizPage(HttpServletRequest request, HttpServletResponse response) {
		String bizTypCd = request.getParameter("bizTypCd");
		String bizCd = request.getParameter("bizCd");
		ModelAndView view = new ModelAndView("/demo/demo/dynamicBizPage");
		if(StringUtils.isNotEmpty(bizTypCd) && StringUtils.isNotEmpty(bizCd)){
			//解析VAL 
			view.addObject("bizTypCd", bizTypCd);
			view.addObject("bizCd", bizCd);
		}else{
			view.addObject("bizTypCd", "");
			view.addObject("bizCd", "");
		}
		return view;
	}
	
	@RequestMapping(value = "/toDynamicSelect01BizPage.do", method = { RequestMethod.GET,RequestMethod.POST })
	public ModelAndView toDynamicSelect01BizPage(HttpServletRequest request, HttpServletResponse response) {
		String bizTypCd = request.getParameter("bizTypCd");
		String bizCd = request.getParameter("bizCd");
		ModelAndView view = new ModelAndView("/demo/demo/dynamicSelect01BizPage");
		view.addObject("bizTypCd", bizTypCd);
		view.addObject("bizCd", bizCd);
		
		
		/*SysCdMapDQueryCondition condition = new SysCdMapDQueryCondition();
		condition.setmTypCd("M0001");
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
		List<String> orgCds = new ArrayList<>();
		orgCds.add(CurrentThreadContext.getCurrentOrgCd());
		condition.setOrgCds(orgCds);
		List<SysCdMapDDTO> list = this.systemApiService.searchSysCdMapDWithTargetList(condition);
		for (SysCdMapDDTO sysCdMapDDTO : list) {
			BaseLogUtils.println("名称：" + sysCdMapDDTO.getTCodeNm());
		}*/
		return view;
	}
	
	/**
	 * 到主页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toMainPage.do", method = { RequestMethod.GET, RequestMethod.POST })//
	public ModelAndView toMainPage(HttpServletRequest request, HttpServletResponse response) {
		//系统日志-有用户的情况下
		BaseLogUtils.info(logger, "toListPage", "加载Demo列表页面.", CurrentThreadContext.getCurrentUserName());
		//系统日志-无用户的情况下
		BaseLogUtils.info(logger, "add", "新增Demo数据.");
		
		ModelAndView view = new ModelAndView("demo/demo/demoMain");
		return view;
	}

	/**
	 * 到新增修改页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("demo/demo/demoModify");
		view.addObject("id", id);
		return view;
	}

	/**
	 * 初始化列表
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		DemoQueryCondition qc = new DemoQueryCondition();
		if (StringUtils.isNotEmpty(searchName)) {
			qc.setName(searchName);
		}
		page.setCondition(qc);

		Pager pager = this.demoService.searchListPage(page);
		List<DemoDTO> list = (List<DemoDTO>) pager.getResultList();
		ListPageVO<DemoDTO> listPage = new ListPageVO<DemoDTO>(list, pager.getTotalCount());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/remove.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public DataResultVO remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO();
		try {
			this.demoService.removeById(Long.valueOf(id));
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo(SysLogConstant.LOG_CMM_DELETE_SUCC);
			BaseLogUtils.info(logger, "remove", "删除数据,主键["+id+"].", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			logger.error("", ex);
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "删除数据失败,主键["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "remove", "删除数据失败,主键["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDemoById.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getDemoById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		DemoVO demoVO = new DemoVO();
		try {
			demoVO = this.demoService.selectById(Long.valueOf(id));
		}catch (BaseException ex) {
			//logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			//logger.error(SysLogConstant.LOG_CMM_SYS_ERROR, ex);
		}
		return demoVO;
	}

	/**
	 * 新增
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addDemo.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object addDemo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO();
		try {
			DemoVO entity = new DemoVO();
			entity.setName(name);
			this.demoService.add(entity);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo(SysLogConstant.LOG_CMM_ADD_SUCC);
			BaseLogUtils.info(logger, "addDemo", "新增数据,name["+name+"].", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addDemo", "新增数据失败,name["+name+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addDemo", "新增数据失败,name["+name+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO();
		try {
			DemoVO entity = new DemoVO();
			entity.setId(Long.valueOf(id));
			entity.setName(name);
			this.demoService.modify(entity);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo(SysLogConstant.LOG_CMM_UPDATE_SUCC);
			BaseLogUtils.info(logger, "modify", "修改数据,id["+id+"].", CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "modify", "修改数据失败,id["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改数据失败,id["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	@RequestMapping(value = "/searchSysCdMapDWithTargetAtomForSelect.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object searchSysCdMapDWithTargetAtomForSelect(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String mTypCd = request.getParameter("mTypCd");
		String sGrpCd = request.getParameter("sGrpCd");
		String sCode = request.getParameter("sCode");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(mTypCd) || StringUtils.isEmpty(sGrpCd) || StringUtils.isEmpty(sCode)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_PARAM_IS_BAD);
			return result;
		}
		SysCdMapDQueryCondition condition = new SysCdMapDQueryCondition();
		try {
			condition.setmTypCd(mTypCd);
			condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
			/*condition.setsGrpCd(sGrpCd);*/
			condition.setsCode(sCode);
			List<SysCdMapDVO> list = this.demoService.searchSysCdMapDWithTargetAtomList(condition);
			JSONArray array = new JSONArray();
			if(CollectionUtils.isNotEmpty(list)){
				net.sf.json.JSONObject obj = null;
				for (SysCdMapDVO sysCdMapDVO : list) {
					obj = new net.sf.json.JSONObject();
					obj.put("sGrpCd", sysCdMapDVO.getSGrpCd());
					obj.put("sCode", sysCdMapDVO.getSCode());
					obj.put("tCode", sysCdMapDVO.getTCode());
					obj.put("tCodeNm", sysCdMapDVO.getTCodeNm());
					array.add(obj);
				}
			}
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo(SysLogConstant.LOG_CMM_ADD_SUCC);
			result.setData(array);
			BaseLogUtils.info(logger, "searchSysCdMapDWithTargetAtomForSelect", "查询数据,DATA:" + JSONObject.toJSONString(condition), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "searchSysCdMapDWithTargetAtomForSelect", "查询数据,DATA:" + JSONObject.toJSONString(condition) + ", ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "searchSysCdMapDWithTargetAtomForSelect", "查询数据,DATA:" + JSONObject.toJSONString(condition) + ", ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
}
